[1]:
%run ../initscript.py
HTML("""
<div id="popup" style="padding-bottom:5px; display:none;">
<div>Enter Password:</div>
<input id="password" type="password"/>
<button onclick="done()" style="border-radius: 12px;">Submit</button>
</div>
<button onclick="unlock()" style="border-radius: 12px;">Unclock</button>
<a href="#" onclick="code_toggle(this); return false;">show code</a>
""")
[1]:
[2]:
%run loadtsfuncs.py
from ipywidgets import *
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
toggle()
[2]:
Exponential Smoothing Forecasts¶
Simple Exponential Smoothing¶
[3]:
def ses_forecast(forecasts, holdouts, level, optimized):
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
df_house.index.freq = 'MS'
plt.figure(figsize=(12, 6))
if holdouts==0:
train, test = df_house.iloc[:, 0], []
model = SimpleExpSmoothing(train).fit(smoothing_level=level, optimized=optimized)
pred = model.predict(start=train.index[0], end=train.index[-1] + forecasts*df_house.index.freq)
else:
train, test = df_house.iloc[:-holdouts, 0], df_house.iloc[-holdouts:, 0]
model = SimpleExpSmoothing(train).fit(smoothing_level=level, optimized=optimized)
pred = model.predict(start=train.index[0], end=test.index[-1] + forecasts*df_house.index.freq)
plt.plot(test.index, test, label='Holdouts', c='fuchsia')
plt.plot(train.index, train, label='Train', c='cornflowerblue')
plt.plot(pred.index, pred, label='Simple Exponential Smoothing', c='orange')
plt.legend(loc='best')
plt.title('House Sales')
plt.show()
widgets.interact_manual.opts['manual_name'] = 'Run Forecast'
interact_manual(ses_forecast,
forecasts=widgets.BoundedIntText(value=12, min=1, description='Forecasts:', disabled=False),
holdouts=widgets.BoundedIntText(value=0, min=0, description='Holdouts:', disabled=False),
level=widgets.BoundedFloatText(value=0.2, min=0, max=1, step=0.05, description='Level:', disabled=False),
optimized=widgets.Checkbox(value=False, description='Optimize Parameters', disabled=False))
plt.show()
toggle()
[3]:
Holt’s Model for Trend¶
[4]:
from statsmodels.tsa.holtwinters import Holt
# df_house.index.freq = 'MS'
plt.figure(figsize=(12, 6))
train = df_house.iloc[:, 0]
train.index = pd.DatetimeIndex(train.index.values, freq=train.index.inferred_freq)
model = Holt(train).fit(smoothing_level=0.2, smoothing_slope=0.2, optimized=False)
pred = model.predict(start=train.index[0], end=train.index[-1] + 12*train.index.freq)
plt.plot(train.index, train, label='Train', c='cornflowerblue')
plt.plot(pred.index, pred, label='Holt\'s', c='orange')
plt.legend(loc='best')
plt.title('House Sales')
plt.show()
toggle()
[4]:
Winters’ Exponential Smoothing Model¶
[5]:
from statsmodels.tsa.holtwinters import ExponentialSmoothing
plt.figure(figsize=(12, 6))
train, test = df_drink.iloc[:-8, 0], df_drink.iloc[-8:,0]
train.index = pd.DatetimeIndex(train.index.values, freq=train.index.inferred_freq)
model = ExponentialSmoothing(train, seasonal='mul', seasonal_periods=4).fit(smoothing_level=0.2,
smoothing_slope=0.2,
smoothing_seasonal=0.4,
optimized=False)
pred = model.predict(start=train.index[0], end=test.index[-1] + 4*train.index.freq)
plt.plot(train.index, train, label='Train', c='cornflowerblue')
plt.plot(test.index, test, label='Holdouts', c='fuchsia')
plt.plot(pred.index, pred, label='Winters\'', c='orange')
plt.legend(loc='best')
plt.title('Drink Sales')
plt.show()
toggle()
[5]: